package com.john.market.activities;

import java.util.UUID;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.john.market.R;
import com.john.market.classes.Product;
import com.john.market.classes.ProductAdapter;
import com.john.market.classes.UpdatableListActivity;
import com.john.market.interfaces.IBuyer;
import com.john.market.interfaces.IShop;

/**
 * Activity that showing products for sale,
 * where user can buy products
 * 
 */
public class ShopActivity extends UpdatableListActivity implements IShop {
	// Buyer object
	private IBuyer mBuyer = null;
	
	// Listener for the "Buy" button 
	private OnClickListener mOnBuyClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			// Getting product ID 
			UUID id = (UUID)view.getTag();
			
			// Getting product and purchase it
			int pos = getProductPos(id);
			if (pos != -1) {
				Product product = mProductList.get(pos);
				mBuyer.buyProduct(product, 1);
			}
			
		}
		
	};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop);
        
        // Creating adapter for the ListView
        this.mAdapter = new ProductAdapter(this, R.layout.product, this.mProductList, "Buy", true);
        this.mAdapter.setOnClickListener(this.mOnBuyClickListener);
        
        // Setting the list adapter
        setListAdapter(this.mAdapter);
        
	}
	
	@Override
	public void setBuyer(IBuyer buyer) {
		this.mBuyer = buyer;
		
	}
	
}
